<?php

namespace app\admin\controller;

use app\admin\model\About as AboutModel;
use app\common\Cache\AboutCache;

class About extends AdminBase
{
    //
    public function getList()
    {
        $data = model('about')->getAbout(10);

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("keywords", input('param.keywords', ''));
        return $this->fetch();
    }

    /**
     * @Title: add
     * @Description: 关于添加
     * @return mixed|void
     * @Author: 李军伟
     */
    public function add()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['title'])) {
                $this->error('标题不可为空');
            }
            if (empty($input['content'])) {
                $this->error('内容不可为空');
            }
            if (!isset($input['type'])) {
                $this->error('类型不可为空');
            }
            if (!isset($input['language'])) {
                $this->error('语言不可为空');
            }
            $insert_data = array(
                'type' => $input['type'],
                'title' => $input['title'],
                'author' => $input['author'],
                'introduce' => $input['introduce'],
                'language' => $input['language'],
                'content' => $input['content'],
                'cover_image' => $input['cover_image'],
                'pub_time' => strtotime($input['pub_time']),
                'created_time' => time()
            );
            $res = model('about')->insert($insert_data);
            if ($res > 0) {
                AboutCache::delAboutCache();
                $this->success('添加成功', 'getList');
            } else {
                $this->error('添加失败，请重试');
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * @Title: del
     * @Description: 文章删除
     * @return mixed
     * @Author: 李军伟
     */
    public function del()
    {
        if (request()->isPost()) {
            $about_id = input("param.id");
            $info = model('about')->delAbout($about_id);
            if ($info > 0) {
                AboutCache::delAboutCache();
                $this->success('删除成功', 'getList');
            } else {
                $this->error('删除失败，请重试');
            }
        }
    }

    /**
     * @Title: update
     * @Description: 文章更新
     * @return mixed|void
     * @Author: 李军伟
     */
    public function update()
    {
        if (request()->isPost()) {
            $input = input();
            if (empty($input['title'])) {
                $this->error('标题不可为空');
            }
            if (empty($input['content'])) {
                $this->error('内容不可为空');
            }
            if (!isset($input['type'])) {
                $this->error('类型不可为空');
            }
            if (!isset($input['language'])) {
                $this->error('语言不可为空');
            }
            $list = array(
                'id' => $input['id'],
                'type' => $input['type'],
                'title' => $input['title'],
                'author' => $input['author'],
                'introduce' => $input['introduce'],
                'language' => $input['language'],
                'content' => $input['content'],
                'cover_image' => $input['cover_image'],
                'pub_time' => strtotime($input['pub_time']),
                'updated_time' => time()
            );
            $res = model('about')->editAbout($input['id'], $list);
            if ($res > 0) {
                AboutCache::delAboutCache();
                $this->success('更新成功', 'getList');
            } else {
                $this->error('更新失败，请重试');
            }
        } else {
            //加载页面
            $input = input();
            $where = ['id' => $input['id']];
            $info = (new AboutModel())->getOne($where);
            // 获取分类列表
            $list = [
                ['id' => 0, 'name' => '使用教程'],
                ['id' => 1, 'name' => '首页公告'],
                ['id' => 2, 'name' => '财经新闻'],
            ];
            $this->assign("domain", $this->domain);
            $this->assign("data", $info);
            $this->assign("list", $list);
            return $this->fetch();
        }
    }
}
